﻿using System;
using System.Buffers;
using Beefly.Json.Exceptions;
using Beefly.Json.Meta;

namespace Beefly.Json.Formats;

sealed class IntPtrJsonFormat : AJsonFormat<IntPtr>
{
    private const int SIZE = 1024;
    
    public override void Serialize(IBufferWriter<char> writer, ref IntPtr item)
    {
        var span = writer.GetSpan(SIZE);
        if (!item.TryFormat(span, out var written))
            throw new NumberFormatWriterException();
        writer.Advance(written);
    }

    public override IntPtr Deserialize(in Span<char> str)
    {
        return IntPtr.Parse(str);
    }
}